<!DOCTYPE html>
<html prefix="og: http://ogp.me/ns#">
<head>
    <meta charset='utf-8'>
    <title>Fresco | 缩放和旋转图片</title>
    <link rel='stylesheet' href='/static/site.css' type='text/css'/>
    <link rel="stylesheet" href='/static/pygments.css' type='text/css'/>
    <link rel='shortcut icon' href='/static/favicon.png'>
    <meta name='viewport' content='width=480'>
    <meta property="og:title" content="缩放和旋转图片" />
    <meta property="og:site_name" content="fresco">
    <meta property='og:description' content='Fresco is an image management library.'>
    <meta property='og:image' content='http://facebook.github.io/fresco/static/fresco-og-image.png'>
    <meta property='og:url' content="http://facebook.github.io/fresco/docs/resizing-rotating.html">
    
    <meta property='og:type' content='website'>
    
    <script type="text/javascript" src="//use.typekit.net/vqa1hcx.js"></script>
    <script type="text/javascript">{'try{Typekit.load();}catch(e){}'}</script>
    <!-- Google Analytics -->
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-43024238-8', 'auto');
      ga('send', 'pageview');

    </script>
</head>
<body>

<header class='topbar'><nav class='width'>
<a href='/'>
  <img src="/static/fresco-logo.png" class="logo">
</a>
    <ul>
      <li><a href="/docs/index.html#_"
          
          class="active" >
          文档
      </a></li>
      <li><a href="/"
          >
          关于
      </a></li>
      <li><a href="/support.html"
          >
          支持
      </a></li>
      <li><a href="/javadoc">
          API
      </a></li>
      <li><a href="http://github.com/facebook/fresco">GitHub</a>
      <li><a href="/about.html"
          >
      关于本文档</a></li>
    </ul>
</nav></header>


<section class='content'><div class='width'>

<nav class='toc'>
  
    <section>
      <h3>配置和使用</h3>
      <ul>
        
          <li>
            <a href="/docs/download-fresco.html#_">
              下载
            </a>
            
          </li>
        
          <li>
            <a href="/docs/index.html#_">
              配置和开始使用
            </a>
            
          </li>
        
          <li>
            <a href="/docs/concepts.html#_">
              关键概念
            </a>
            
          </li>
        
          <li>
            <a href="/docs/supported-uris.html#_">
              支持的 URIs
            </a>
            
          </li>
        
      </ul>
    </section>
  
    <section>
      <h3>Drawee 指南</h3>
      <ul>
        
          <li>
            <a href="/docs/using-drawees-xml.html#_">
              在XML中使用 Drawees
            </a>
            
          </li>
        
          <li>
            <a href="/docs/using-drawees-code.html#_">
              在Java代码中使用 Drawees
            </a>
            
          </li>
        
          <li>
            <a href="/docs/drawee-components.html#_">
              Drawee的各种效果配置
            </a>
            
          </li>
        
          <li>
            <a href="/docs/scaling.html#_">
              缩放
            </a>
            
          </li>
        
          <li>
            <a href="/docs/rounded-corners-and-circles.html#_">
              圆角/圆圈
            </a>
            
          </li>
        
          <li>
            <a href="/docs/using-controllerbuilder.html#_">
              使用ControllerBuilder
            </a>
            
          </li>
        
          <li>
            <a href="/docs/progressive-jpegs.html#_">
              渐进式JPEG图
            </a>
            
          </li>
        
          <li>
            <a href="/docs/animations.html#_">
              动画图(gif)
            </a>
            
          </li>
        
          <li>
            <a href="/docs/requesting-multiple-images.html#_">
              多图请求及图片复用
            </a>
            
          </li>
        
          <li>
            <a href="/docs/listening-download-events.html#_">
              监听下载事件
            </a>
            
          </li>
        
          <li>
            <a href="/docs/resizing-rotating.html#_" class="active">
              缩放和旋转
            </a>
            
          </li>
        
          <li>
            <a href="/docs/modifying-image.html#_">
              图片的修改
            </a>
            
          </li>
        
          <li>
            <a href="/docs/image-requests.html#_">
              图片请求
            </a>
            
          </li>
        
          <li>
            <a href="/docs/writing-custom-views.html#_">
              自定义View
            </a>
            
          </li>
        
          <li>
            <a href="/docs/gotchas.html#_">
              一些陷阱
            </a>
            
          </li>
        
      </ul>
    </section>
  
    <section>
      <h3>Image Pipeline 指南</h3>
      <ul>
        
          <li>
            <a href="/docs/intro-image-pipeline.html#_">
              什么是Image Pipeline
            </a>
            
          </li>
        
          <li>
            <a href="/docs/configure-image-pipeline.html#_">
              配置Image Pipeline
            </a>
            
          </li>
        
          <li>
            <a href="/docs/caching.html#_">
              缓存的行为控制
            </a>
            
          </li>
        
          <li>
            <a href="/docs/using-image-pipeline.html#_">
              直接使用Image Pipeline
            </a>
            
          </li>
        
          <li>
            <a href="/docs/datasources-datasubscribers.html#_">
              数据源/订阅数据源
            </a>
            
          </li>
        
          <li>
            <a href="/docs/closeable-references.html#_">
              可关闭的引用
            </a>
            
          </li>
        
      </ul>
    </section>
  
    <section>
      <h3>第三方类库</h3>
      <ul>
        
          <li>
            <a href="/docs/using-other-network-layers.html#_">
              自定义网络加载
            </a>
            
          </li>
        
          <li>
            <a href="/docs/using-other-image-loaders.html#_">
              使用其他的Image Loader
            </a>
            
          </li>
        
      </ul>
    </section>
  
</nav>


<article class='withtoc'>
    <h1>
      缩放和旋转图片
      <a class="edit-page-link" href="https://github.com/liaohuqiu/fresco-docs-cn/blob/docs/docs/01-resizing-rotating.md" target="_blank">编辑和纠错</a>
    </h1>
    <p></p>

    <p>使用这个功能需要直接<a href="using-controllerbuilder.html#ImageRequest">创建 image request</a>。</p>

<h2>缩放图片</h2>

<h3>什么时候该修改图片尺寸</h3>

<p>一般地，当所要显示的图片和显示区域大小不一致时，会按以下方式进行处理。</p>

<ol>
<li>从服务器下载小一些的图片</li>
<li>显示时缩放图片</li>
<li>调整图片尺寸大小</li>
</ol>

<p>对于一个图片，如果服务器支持不同尺寸的缩略图，那么每次下载都选择尺寸最匹配的图片，这个不仅节省数据流量也节约本地储存和CPU。</p>

<p>如果服务器不支持，或者处理本地图片的话，第二个选择是<a href="scaling.html">使用缩放类型</a>。缩放是用Androi内置的功能使图像和显示边界相符。在4.0之后，支持硬件加速。这在大部分情况下是最快，同时也是最高效的显示一张和显示边界大小相符的图片的方式。首先指定<code>layout_width</code>和<code>layout_width</code>为指定值，然后指定<a href="scaling.html">缩放类型</a></p>

<p>但当所要显示的图片比显示区域大许多的时候，不推荐这样做，缩放过程会导致大量的内存消耗。</p>

<p>这时，需要改变图片尺寸。</p>

<h3>修改图片尺寸</h3>

<p>调整大小并不是修改原来的文件，而是在解码之前，在native内存中修改。</p>

<p>这个缩放方法，比Android内置的缩放范围更大。Android相机生成的照片一般尺寸都很大，需要调整大小之后才能被显示。</p>

<p>目前，仅仅支持JPEG格式的图片，同时，大部分的Android系统相机图片都是JPEG的。</p>

<p>如果要修改图片尺寸，创建<code>ImageRequest</code>时，提供一个 <a href="../javadoc/reference/com/facebook/imagepipeline/common/ResizeOptions.html">ResizeOptions</a>:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">Uri</span> <span class="n">uri</span> <span class="o">=</span> <span class="s">&quot;file:///mnt/sdcard/MyApp/myfile.jpg&quot;</span><span class="o">;</span>
<span class="kt">int</span> <span class="n">width</span> <span class="o">=</span> <span class="mi">50</span><span class="o">,</span> <span class="n">height</span> <span class="o">=</span> <span class="mi">50</span><span class="o">;</span>
<span class="n">ImageRequest</span> <span class="n">request</span> <span class="o">=</span> <span class="n">ImageRequestBuilder</span><span class="o">.</span><span class="na">newBuilderWithSource</span><span class="o">(</span><span class="n">uri</span><span class="o">)</span>
    <span class="o">.</span><span class="na">setResizeOptions</span><span class="o">(</span><span class="k">new</span> <span class="nf">ResizeOptions</span><span class="o">(</span><span class="n">width</span><span class="o">,</span> <span class="n">height</span><span class="o">))</span>
    <span class="o">.</span><span class="na">build</span><span class="o">();</span>
<span class="n">PipelineDraweeController</span> <span class="n">controller</span> <span class="o">=</span> <span class="n">Fresco</span><span class="o">.</span><span class="na">newDraweeControllerBuilder</span><span class="o">()</span>
    <span class="o">.</span><span class="na">setOldController</span><span class="o">(</span><span class="n">mDraweeView</span><span class="o">.</span><span class="na">getController</span><span class="o">())</span>
    <span class="o">.</span><span class="na">setImageRequest</span><span class="o">(</span><span class="n">request</span><span class="o">)</span>
    <span class="o">.</span><span class="na">build</span><span class="o">();</span>
<span class="n">mSimpleDraweeView</span><span class="o">.</span><span class="na">setController</span><span class="o">(</span><span class="n">controller</span><span class="o">);</span>
</code></pre></div>
<h2><a name="rotate"></a>自动旋转</h2>

<p>如果看到的图片是侧着的，用户是难受的。许多设备会在JPEG文件的metadata中记录下照片的方向。如果你想图片呈现的方向和设备屏幕的方向一致，你可以简单地这样做到:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">ImageRequest</span> <span class="n">request</span> <span class="o">=</span> <span class="n">ImageRequestBuilder</span><span class="o">.</span><span class="na">newBuilderWithSource</span><span class="o">(</span><span class="n">uri</span><span class="o">)</span>
    <span class="o">.</span><span class="na">setAutoRotateEnabled</span><span class="o">(</span><span class="kc">true</span><span class="o">)</span>
    <span class="o">.</span><span class="na">build</span><span class="o">();</span>
<span class="c1">// as above</span>
</code></pre></div>

    <div class="docs-prevnext">
      
        <a href="/docs/listening-download-events.html#_">&larr; 上一页</a>
      
      
        <a class="right" href="/docs/modifying-image.html#_">下一页 &rarr;</a>
      
    </div>

    <a id="_"></a>
</article>

</div></section>
<footer><div class='width'>
    &copy; Copyright 2015, Facebook Inc.
</div></footer>

<script src="/static/linkify.js"></script>
<!-- Google Analytics -->
</body>
</html>

